package com.handpay.framework.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handpay.framework.utils.BitmapLoaderNew;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.other.activity.FullScreenImageActivty;
import com.handpay.zztong.hp.log.HPLog;
import com.nostra13.universalimageloader.core.assist.FailReason;

/**
 * 
 * @author whyuan 提交照片资料相片的自定义view。需要实现具体业务逻辑
 */

public class QuailficationPhotoView extends RelativeLayout {

	private LayoutInflater mInflater;

	public enum Statu {
		NoUpload, Upload, UploadSuccess, UploadFailed
	}

	private Statu mStatu;
	public boolean isLoadSuccess = false;
	private int mDefaultPicShown=R.drawable.placephoto;
	private int mDefaultLargeShown;
	private ImageView mIvTip;
	private ImageView mIvPhoto;
	private String mOutputFileUri;
	// private Bitmap mBitmap;
	private UploadClickListener mUpListener;
	private Context mContext;
	private int position = -1;
	TextView tvContentDes;
	
	public void setPosition(int position) {
		this.position = position;
	}

	public int getPosition() {
		return position;
	}
	public void deleteView(){
		mOutputFileUri = null;
		mIvPhoto.setVisibility(View.VISIBLE);
		tvContentDes.setVisibility(View.VISIBLE);
		mIvPhoto.setImageResource(mDefaultPicShown);
		mIvTip.setVisibility(View.GONE);
		mStatu = Statu.NoUpload;
	}
	public QuailficationPhotoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mInflater.inflate(R.layout.quailification_pic, this);
		mIvTip = (ImageView) this.findViewById(R.id.tip);
		// 点击右上角。问号的状态，显示默认图片；删除状态，删除当前图片.其他状态点击无效
		mIvTip.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(null != mUpListener){
					mUpListener.onDelectClickListener(QuailficationPhotoView.this);
				}
			}
		});
		// 点击上传，进入上传。
//		mIvUpload = (ImageView) this.findViewById(R.id.iv_upload);
		/*mIvUpload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != mUpListener) {
					mUpListener.onUploadClickListener(QuailficationPhotoView.this);
				}
			}
		});*/
		// 当里面没图片的时候，不反应。当有图片的时候，进入大图
		mIvPhoto = (ImageView) this.findViewById(R.id.iv_photo);
		mIvPhoto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(TextUtils.isEmpty(mOutputFileUri)){
					if (null != mUpListener) {
						mUpListener.onUploadClickListener(QuailficationPhotoView.this);
					}
				}else{
					Intent intent = new Intent(QuailficationPhotoView.this
							.getContext(), FullScreenImageActivty.class);
					intent.putExtra(FullScreenImageActivty.KEY_PIC_URI,
							mOutputFileUri);
					QuailficationPhotoView.this.getContext().startActivity(intent);
				}
			}
		});
	}

	public void setOptional(boolean optional) {
		/*if (optional) {
			this.findViewById(R.id.tv_wenhao).setVisibility(View.GONE);
		} else {
			this.findViewById(R.id.tv_wenhao).setVisibility(View.VISIBLE);
		}*/
	}
	
	public void init(String picUri, boolean success,
			UploadClickListener uploadListener) {
		init(mContext.getString(R.string.text_picview), R.drawable.placephoto, R.drawable.placephoto, picUri, success, uploadListener);
	}
	/**
	 * 
	 * @param contentDes
	 *            默认描述
	 * @param defaultPicShown
	 *            示例图片id
	 * @param picUri
	 *            如果不为空，表示是带着图片进来的。
	 * @param success
	 *            和picUri和用，表示这张图片是通过的还是失败的
	 * @param uploadListener
	 *            用户点击上传，如何操作？
	 */
	public void init(String contentDes, int defaultPicShown,
			int defaultPicLargeShown, String picUri, boolean success,
			UploadClickListener uploadListener) {
		Log.i("result", "路径："+picUri);
		tvContentDes = (TextView) findViewById(R.id.contentDes);
		tvContentDes.setText(contentDes);
		mDefaultPicShown = defaultPicShown;
		mDefaultLargeShown = defaultPicLargeShown;
		mUpListener = uploadListener;
//		mIvUpload.setVisibility(View.VISIBLE);
		if (!TextUtils.isEmpty(picUri)) {
			tvContentDes.setVisibility(View.GONE);
			mIvPhoto.setImageResource(defaultPicShown);
			mOutputFileUri = picUri;
			if (success) {
//				mIvTip.setImageResource(R.drawable.success);
				mStatu = Statu.UploadSuccess;
			} else {
//				mIvTip.setImageResource(R.drawable.warn);
				mStatu = Statu.UploadFailed;
			}
			// 确保该view的大小不可能为0
			measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			
			BitmapLoaderNew.BitmapLoaderListener listener = new BitmapLoaderNew.BitmapLoaderListener() {

				@Override
				public void onLoadingComplete(String imageUri, View view,
						Bitmap loadedImage) {
					loadedImage = ThumbnailUtils.extractThumbnail(loadedImage,
							mIvPhoto.getMeasuredWidth(),
							mIvPhoto.getMeasuredHeight());
					isLoadSuccess = true;
					mIvPhoto.clearFocus();
					mIvPhoto.setImageBitmap(loadedImage);
					visiabletip();
				}
				@Override
				public void onLoadingFailed(String imageUri, View view,
						FailReason failReason) {
					super.onLoadingFailed(imageUri, view, failReason);
					mIvPhoto.setVisibility(View.INVISIBLE);
					visiabletip();
				}
				public void visiabletip(){
					if(mOutputFileUri.startsWith("http:")){
						mIvTip.setVisibility(View.INVISIBLE);
					}else{
						mIvTip.setVisibility(View.VISIBLE);
					}
				}
			};
			BitmapLoaderNew.getInstance(getContext()).loadImage(picUri, listener);
		} else {
			tvContentDes.setVisibility(View.VISIBLE);
			mIvPhoto.setImageResource(defaultPicShown);
			mIvTip.setVisibility(View.INVISIBLE);
			mStatu = Statu.NoUpload;
		}
	}


	public void setPic(String uri) {
		mOutputFileUri = uri;
		// 创建thumbnail显示在iv_photo上。并将tip的图标改成删除图标
		HPLog.i("time", "before loadBitmap:" + SystemClock.elapsedRealtime()
				+ uri);
		BitmapLoaderNew.BitmapLoaderListener listener = new BitmapLoaderNew.BitmapLoaderListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view,
					Bitmap loadedImage) {
				loadedImage = ThumbnailUtils.extractThumbnail(loadedImage,
						mIvPhoto.getMeasuredWidth(),
						mIvPhoto.getMeasuredHeight());
				isLoadSuccess = true;
				mIvPhoto.setImageBitmap(loadedImage);
			}
		};
		BitmapLoaderNew.getInstance(getContext()).loadImage(uri, listener);
		mIvTip.setVisibility(View.VISIBLE);
		mIvTip.setImageResource(R.drawable.clear);
		mStatu = Statu.Upload;
	}

	public String getPicUri() {
		return mOutputFileUri;
	}

	public interface UploadClickListener {
		void onUploadClickListener(QuailficationPhotoView view);
		void onDelectClickListener(QuailficationPhotoView view);
	}
	
	public void setViewVisable(int visibility){
		mIvPhoto.setVisibility(visibility);
	}

	public String getmOutputFileUri() {
		return mOutputFileUri;
	}

	public void setmOutputFileUri(String mOutputFileUri) {
		this.mOutputFileUri = mOutputFileUri;
	}
	
}
